<template>
    <div></div>
</template>

<script>
import LoginApi from "@/api/login";
import BpmApi from "@/api/rxBpm/bpmInst.js";
export default {
    name: "jumpInst",
    data(){
        return{
            isMobile: false,
            params:{}
        }
    },
    created() {
        this.checkDevice();

    },
    methods: {
        checkDevice() {
            const userAgent = navigator.userAgent;
            const mobileRegex = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
            this.isMobile = mobileRegex.test(userAgent);
            // 获取当前页面的 URL
            const url = window.location.href;
            const urlObj = new URL(url);
            const urlParams = new URLSearchParams(urlObj.search);
            const instId = urlParams.get('instId'); // 1901920559464345601
            const entwxid = urlParams.get('entwxid'); // LiMeiHua
            const tenantId = urlParams.get('tenantId'); // 1
            this.params = {
                instId:instId,
                entwxid:entwxid,
                tenantId:tenantId
            }
            console.log('消息链接的租户tenantId=='+instId)
            if(tenantId){
                uni.setStorage({key: "tenantId", data:tenantId});
            }
            if(this.isMobile){
                //移动端
                this.navigateTo(this.params)
            }else{
                //pc端
                this.encryptInstIdPc()
            }
        },
        bpmDetails(obj,action) {
            let params = {
                instId: obj.instId,
                routerName:'openDoc',
                action:action
            }
            if(obj.taskId){
                params.taskId = obj.taskId;
            }
            let mobileAppId = uni.getStorageSync("mobileAppId");
            let appId =''
            if(obj.appId){
                appId=obj.appId?obj.appId:mobileAppId
            }
            uni.reLaunch({
                url:'/views/modules/bpm/OpenDoc?params='+ JSON.stringify(params) +"&appId="+appId
            })
        },
        navigateTo(item) {
            let self = this;
            console.log("---待办已办跳转：navigateTo item==" + JSON.stringify(item));
            BpmApi.getInfoByDefKeyInstId("openDoc", item.instId).then(res => {
                console.log("---待办已办跳转：res==" + JSON.stringify(res));
                if (res.action == "task") {
                    self.bpmDetails(res,'edit');
                }
                if (res.action == "detail") {
                    self.bpmDetails(res,'detail');
                }
            });
        },
        encryptInstIdPc(){
            let self = this;
            this.encrypPC(this.params.instId,function(encryptInstId){
                let appId=encryptInstId.appId;
                encryptInstId=encryptInstId.instId;
                let url  = window.location.origin +'/main/openDoc/'+encryptInstId+'?appId='+appId;
                window.location.href = url
            })
        },
        encrypPC(instId,callback){
            LoginApi.getBpmInstEncrypt(instId).then(res=>{
                if(!res.success){
                    this.$message.warn(res.message);
                    return;
                }
                callback(res.data);
            })
        },
    }
}
</script>

<style scoped>

</style>
